$(document).ready(function() {
	// 表单验证
	$("#demo").validate({
		rules:{
			"roles.roleName": {
				required: true,
	    		maxlength: 30
			},
			"roles.remark":{
				required: false,
	    		maxlength: 500
			},
			"operIdList":"required"
		},
		/* 设置错误信息 */
		messages : {
			"operIdList" : {
				required :"至少选择一项"
			}
		}
//		errorPlacement: function(error, element) {   
//	        if (element.attr("name") == "roles.operId" )  {
//	            error.appendTo("#errMsg"); 
//	        } 
//	        else {    
//	            error.insertAfter(element); 
//	        }
//		}
	});
	
	// 查询所有模块
	queryOperationsList(operIdArray);
});

// 检查是否全部为空，不为空将operId组合成字符串
function checkSelectList() {
	var str = document.getElementById("operIdList");
	var collection = "";
	for (i = 0; i < str.options.length; i++) {
		if (str[i].selected == true) {
			collection += str[i].value + ",";
		}
	}
	if (collection == "") {
		alert("至少选择一个模块！");
	} else {
		collection = collection.substring(0, collection.lastIndexOf(','));
		document.getElementById("roles.operCollection").value = collection;

	}
}

// 返回roles.operId的json数据
function queryOperationsList(operIdArray){
	$.post(
			"../AjaxResultData/listAllOperations.action",
			function(data){
				var operationsData = eval("("+data.operationsData+")");  
				for(var i = 0;i<operationsData.length;i++){
					var flag = 0; 
					for(j=0;j<operIdArray.length;j++){
						if(operationsData[i].operId == operIdArray[j]){
							flag = 1;
						}
					}
					if(flag==1){
						$("#operIdList").append("<option value='"+operationsData[i].operId+"' name='operId' selected='selected'>"+operationsData[i].operName+"</option>");
					}else{
						$("#operIdList").append("<option value='"+operationsData[i].operId+"' name='operId'>"+operationsData[i].operName+"</option>");
					}
				}
			},
			"json"
		);
}